<template>
  <!-- 页面布局 -->
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="logo" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="warning" plain @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧导航栏 -->
      <el-aside width="!isCollapse ? '65px' : '200px' ">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="this.activePath = this._routerRoot._route.fullPath"
        >
          <div class="toggle-button" @click="toggleMenu">|||</div>
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <!-- 动态获取字体图标  v-bind动态绑定-->
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="activeHandle"
            >
              <!-- 使用element图标 -->
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧导航栏 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 定义一个变量获取get的数据
      menulist: [],
      // 创建一个iconsObj 图标数据 键是每一个分类的id 值是icon的图标
      // 每次循环的时候，使用iconsObj[item.id] 的方式获取icon 图标 从而渲染图标
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制分类是否展开
      isCollapse: false,
      // 当前激活的分类
      activePath: ''
    }
  },
  // created 只会触发一次
  created() {
    // 调用数据
    this.getmenus()
    // this.activePath = this._routerRoot._route.fullPath
  },
  // beforeUpdate() {
  //   this.activePath = this._routerRoot._route.fullPath
  // },
  // updated() {
  //   this.activePath = this._routerRoot._route.fullPath
  //   console.log(this)
  // },
  methods: {
    // 保存当前激活的分类
    activeHandle(active) {
      this.activePath = this._routerRoot._route.fullPath
    },
    // 展开折叠分类
    toggleMenu() {
      this.isCollapse = !this.isCollapse
    },

    async getmenus() {
      // 发送请求获取get 左侧数据
      const { data: res } = await this.$http.get('/menus')
      // 判断是否为200 进行提示
      if (res.meta.status !== 200) return this.$message.error('res.meta.msg')
      this.menulist = res.data
      // 将获取的左侧数据传递个menus中
    },
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 0px;
  color: #ffffff;

  div {
    display: flex;
    align-items: center;
    font-size: 18px;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
  .iconfont {
    // 调整字体图标与文字的距离
    margin-right: 10px;
  }
}

.el-main {
  background-color: #eaedf1;
}

.toggle-button {
  width: 200px;
  background-color: black;
  color: #fff;
  text-align: center;
  cursor: pointer;
}
</style>
